package com.whc.controller;

import com.whc.entity.Friend;
import com.whc.result.Result;
import com.whc.service.FriendService;
import com.whc.service.UserService;
import com.whc.vo.FriendVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author 韦红材
 * @ClassName FriendController
 * @data 2024/12/10 16:41
 * @Version V1.0
 **/
@Api(tags = "朋友接口")
@RestController
@RequestMapping("/client/friend")
@Slf4j
public class FriendController {
    @Autowired
    private FriendService friendService;
    @Autowired
    private UserService userService;
    @ApiOperation("添加朋友关系")
    @PutMapping("/addFriend")
    @PreAuthorize("hasAuthority('user')")
    public Result addFriend(Long pid,Long fid){
        //判断是否已经是朋友关系，是就直接返回
        List<FriendVo> friends = userService.getAllFriends(pid);
        for(FriendVo f:friends){
            if(f.getId()==fid){
                return Result.success();
            }
        }
        friendService.addFriend(pid,fid);
        return Result.success();
    }

}
